﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using MReviewerClient.MReviewerSvc;
using System.Windows.Navigation;
using MReviewerClient.Models;
using System.Collections.ObjectModel;
using MReviewerClient.Providers;

namespace MReviewerClient.Views
{
    public partial class ListAllReviewView : PhoneApplicationPage
    {
        #region Fields

        /// <summary>
        /// The selected Establishment
        /// </summary>
        public EstablishmentDTO SelectedEstablishment { get; set; }

        /// <summary>
        /// The list of reviews
        /// </summary>
        public ObservableCollection<ReviewDTO> Reviews { get; set; }

        #endregion

        #region Constructor

        /// <summary>
        /// The default constructor
        /// </summary>
        public ListAllReviewView()
        {
            InitializeComponent();  
        }

        #endregion

        #region Methods & Handlers

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            // Select the specified establishment
            string id = NavigationContext.QueryString["id"];
            this.SelectedEstablishment = EstablishmentModel.Instance.SelectByID(id);

            // This action will be executed when the operation for selecting from service is completely
            Action actionUpdateContext = () =>
            {
                this.Reviews = ReviewModel.Instance.Reviews;
                // Update more information of the specified establishment
                this.SelectedEstablishment.AverageRating = ReviewModel.Instance.AverageRating;
                this.SelectedEstablishment.NumReviewer = ReviewModel.Instance.NumReviewer;
                this.DataContext = this;
            };  

            // Select all of reviews of specified establishment
            ReviewModel.Instance.SelectByEstablishment(id, actionUpdateContext);    
        }

        private void menAddNewReview_Click(object sender, EventArgs e)
        {
            if (this.SelectedEstablishment != null)
            {
                NavigationService.Navigate(new Uri(string.Format("/Views/AddReviewDetailView.xaml?id={0}",
                   this.SelectedEstablishment.ID), UriKind.RelativeOrAbsolute));
            }
        }

        #endregion
    }
}